package com.wpam.scrabble.mobile.ui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;

import com.wpam.scrabble.mobile.GameResources;
import com.wpam.scrabble.mobile.LetterManager;

public class SquareBitmap extends Drawable {
	
	public static final int DEFAULT_SIZE = 36;

	public static final int NORMAL = 0;
	public static final int LETTERx2 = 1;
	public static final int LETTERx3 = 2;
	public static final int WORDx2 = 3;
	public static final int WORDx3 = 4;
	public static final int BLANK = 5;
	
	protected Bitmap background_;
	protected Paint letterPaint_;
	protected Paint smallLetterPaint_;
	protected Paint bitmapPaint_;
	protected int posX_;
	protected int posY_;
	protected int size_;
	private boolean accepted_;
	
	private int type_;
	protected String letter_;
	protected String points_;

	protected int letterOffsetX_;
	protected int letterOffsetY_;

	protected int pointOffsetX_;
	protected int pointOffsetY_;
	
	private LightingColorFilter wrongColorFilter_;
	private LightingColorFilter correctColorFilter_;
	private LightingColorFilter lastColorFilter_;
	private LightingColorFilter previousColorFilter_;
	
	public SquareBitmap(int x, int y, int type) {
		
		this.size_ = DEFAULT_SIZE;
		this.type_ = type;
		this.posX_ = x;
		this.posY_ = y;
		init();
	}
	
	public SquareBitmap(int x, int y, int size, int type) {
		
		this.size_ = size;
		this.type_ = type;
		this.posX_ = x;
		this.posY_ = y;
		init();
	}

	public void reset() {
		
		switch (type_) {
		case NORMAL:
			this.background_ = GameResources.getInstance().getField();
			break;
		case LETTERx2:
			this.background_ = GameResources.getInstance().getLetter2();
			break;
		case LETTERx3:
			this.background_ = GameResources.getInstance().getLetter3();
			break;
		case WORDx2:
			this.background_ = GameResources.getInstance().getWord2();
			break;
		case WORDx3:
			this.background_ = GameResources.getInstance().getWord3();
			break;
		case BLANK:
			this.background_ = GameResources.getInstance().getBlank();
			break;
		}
		background_ = Bitmap.createScaledBitmap(background_, size_, size_, true);
		this.letter_ = "";
		this.points_ = "0";
		this.accepted_ = false;
	}
	
	private void init() {

		this.letterPaint_ = new Paint();
		letterPaint_.setStyle(Style.FILL);
		letterPaint_.setAntiAlias(true);
		letterPaint_.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		letterPaint_.setTextAlign(Align.CENTER);
		letterPaint_.setColor(Color.BLACK);
		this.smallLetterPaint_ = new Paint();
		smallLetterPaint_.setStyle(Style.FILL);
		smallLetterPaint_.setAntiAlias(true);
		smallLetterPaint_.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		smallLetterPaint_.setTextAlign(Align.CENTER);
		smallLetterPaint_.setColor(Color.BLACK);
		adjustTextSize();
		this.bitmapPaint_ = new Paint();
		this.lastColorFilter_ = new LightingColorFilter(0xFF00FFFF, 0x00780000);
		this.letterOffsetX_ = (int) (size_ * 0.45);
		this.letterOffsetY_ = (int) (size_ * 0.85);
		this.pointOffsetX_ = (int) (size_ * 0.85);
		this.pointOffsetY_ = (int) (size_ * 0.9);
		this.wrongColorFilter_ = new LightingColorFilter(0xFFFFFFFF, 0x00FF0000);
		this.correctColorFilter_ = new LightingColorFilter(0xFFFFFFFF, 0x000000FF);
		this.previousColorFilter_ = null;
		reset();
	}
	
	public int getX() {
		return posX_;
	}

	public int getY() {
		return posY_;
	}

	public void setPos(int x, int y) {

		this.posX_ = x;
		this.posY_ = y;
	}
	
	public int getSize() {
		return size_;
	}

	public boolean isLetterSet() {
		return !letter_.equals("");
	}
	
	public boolean isAccepted() {
		return accepted_;
	}
	
	public boolean setLetter(String letter) {
		
		if (!letter_.equals(""))
			return false;
		this.background_ = GameResources.getInstance().getBlank();
		this.background_ = Bitmap.createScaledBitmap(background_, size_, size_, true);
		this.letter_ = letter;
		this.points_ = LetterManager.getPoints(letter_);
		return true;
	}
	
	public String getLetter() {
		return letter_;
	}

	public int getPoints() {
		return (points_.equals("") ? 0 : Integer.valueOf(points_));
	}
	
	public int getType() {
		return type_;
	}
	
	@Override
	public void draw(Canvas canvas) {
		
		canvas.drawBitmap(background_, posX_, posY_, bitmapPaint_);
		if (letter_.length() != 0) {
			canvas.drawText(letter_, posX_ + letterOffsetX_, posY_ + letterOffsetY_, letterPaint_);
			canvas.drawText(points_, posX_ + pointOffsetX_, posY_ + pointOffsetY_, smallLetterPaint_);
		}
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		
		bitmapPaint_.setAlpha(alpha);
		letterPaint_.setAlpha(alpha);
		smallLetterPaint_.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		
		bitmapPaint_.setColorFilter(cf);
		letterPaint_.setColorFilter(cf);
		smallLetterPaint_.setColorFilter(cf);
	}

	public void highlight(boolean state) {
		
		if (state) {
			if (this.letter_.equals(""))
				setColorFilter(correctColorFilter_);
			else
				setColorFilter(wrongColorFilter_);
		} else
			setColorFilter(previousColorFilter_);
	}

	public void markAsLastOuterMove(boolean state) {
		
		if (state) {
			previousColorFilter_ = lastColorFilter_;
			setColorFilter(lastColorFilter_);
		} else {
			previousColorFilter_ = null;
			setColorFilter(null);
		}
	}
	
	public void setAccepted() {
		this.accepted_ = true;
	}
	
	protected void adjustTextSize() {
		letterPaint_.setTextSize(100);
		letterPaint_.setTextScaleX(1.0f);
	    Rect bounds = new Rect();
	    // ask the paint for the bounding rect if it were to draw this
	    // text
	    letterPaint_.getTextBounds("D", 0, 1, bounds);
	 
	    // get the height that would have been produced
	    int h = bounds.bottom - bounds.top;
	 
	    // make the text text up 60% of the height
	    float target = (float)size_*.6f;
	 
	    // figure out what textSize setting would create that height
	    // of text
	    float size  = ((target/h)*100f);
	 
	    // and set it into the paint
	    letterPaint_.setTextSize(size);
		smallLetterPaint_.setTextSize((float) (letterPaint_.getTextSize() / 2.6));
	}
}
